<template>
	<view>
		<!-- 顶部导航 -->
		<fa-navbar title="挂添加" ref="navbar" ></fa-navbar>
		<view class="content">
			<u-form :model="form" ref="uForm">
				<u-form-item label="选择套餐" prop="name" label-width="140">
					<u-input v-model="form.name" placeholder="请选择" @click="show = true"/>
					<u-select v-model="show" mode="single-column" :list="list1" @confirm="confirm"></u-select>
				</u-form-item>
				<!-- <u-form-item label="名称" prop="name" label-width="140">
					<u-input v-model="form.name" />
				</u-form-item> -->
				<!-- <u-form-item label="总价" prop="price" label-width="140">
					<u-input v-model="form.price" />
				</u-form-item> -->
				<u-form-item label="欠款价格" prop="price" label-width="140">
					<u-input v-model="form.qk_price" />
				</u-form-item>
				<!-- <u-form-item label="数量" prop="num" label-width="140">
					<u-input v-model="form.num" />
				</u-form-item> -->
				<u-form-item label="备注" prop="num" label-width="140">
					<u-input v-model="form.bz" />
				</u-form-item>
				<u-form-item label="上传图片" prop="jzqy" label-width="140">
					<u-upload :action="action" :auto-upload="true" @on-success='handleSuccess' @on-remove="handleRemove"></u-upload>
				</u-form-item>
			</u-form>
			<u-button @click="submit" :custom-style="customStyle">添加</u-button>
		</view>
		<u-toast ref="uToast" />
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
	import { archives } from '@/common/fa.mixin.js';
	// import FaUploadImage from '@/pages/publish/components/fa-upload-image/fa-upload-image.vue';
	export default {
		components: {
			// FaUploadImage
		},
		mixins: [archives],
		data() {
			return {
				action: 'http://leiyu99.com/api/common/upload',
				filesArr: [],
				user_id: 0,
				show: false,
				page:1,
				list1: [],
				customStyle: {
					marginTop: '50px', // 注意驼峰命名，并且值必须用引号包括，因为这是对象
					color: '#333',
					border: 'none',
					background: '#e0ebdd',
				},
				form: {
					// id: '',
					name: '',
					num: '',
					bz: '',
					price: 0,
					qk_price: 0,
					fileList: [],
				},
				rules: {
					name: [
						{ 
							required: true, 
							message: '请输入名称', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change'],
						}
					],
					mobile: [
						{
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				}
			};
		},
		onLoad(e) {
			let query = e || {};
			this.user_id = query.user_id || 0;
			this.is_update = true;
			this.getArchivesList()
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			getArchivesList: async function() {
				let data = {
					page: this.page,
					member_id: this.user_id 
				};
				
				let res = await this.$api.myjiaddList(data);
				console.log(res,'1111')
				this.list1 = this.list1.concat(res.data);
				
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过',this.form);
						
						this.$api.addGuaAdd({ 
							user_id:this.user_id,
							name: this.form.name,
							id: this.form.id,
							num: this.form.num,
							bz: this.form.bz,
							price: this.form.price,
							qk_price: this.form.qk_price,
							fileList: this.form.fileList,
						}).then(res => {

							if(res.code == 1){
								uni.showToast({
									title: '添加成功',
									icon: 'success',
									duration: 2000,
									success: function (res1) {
										setTimeout(() => {
											uni.redirectTo({
												url: '/pages/my/user_list'
											});
										}, 2000);
										
										
									},
								});

							}
							
						});
						
					} else {
						console.log('验证失败');
					}
				});
			},

			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				var str = e.toString();
				this.form.goods_ids = str;
			},
			 // 上传成功回调
			handleSuccess(res, index, lists) {
				uni.showToast({
					title: '上传成功',
					icon: 'success'
				});
				// 更新文件列表
				this.form.fileList = this.form.fileList.concat(res.data.url);
				
				console.log(this.form.fileList,'concat')
			},
			// 文件被移除
			handleRemove(index, lists) {
				console.log(index,'index')
				this.form.fileList.splice(index, 1);
				console.log(this.form.fileList,'fileList')
			},
			confirm(e) {
				console.log(e)
				this.form.name = e[0].label;
				this.form.id = e[0].value;

			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.content{
		width: 90%;margin: 10px auto;
	}
</style>
